package com.qf.xiaomi.service.impl;

import com.qf.xiaomi.dao.impl.CartDaoImpl;
import com.qf.xiaomi.pojo.CartUnion;
import com.qf.xiaomi.service.CartService;

import java.sql.SQLException;
import java.util.List;


/**
 * @Created by 马英超 on 2022/8/18 17:32
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class CartServiceImpl implements CartService {
    @Override
    public boolean dealCart(String productId, String productNum, int uid) {
        //参数处理
        int id=Integer.parseInt(productId);
        int num=Integer.parseInt(productNum);
        //调用Dao进行数据的处理
        CartDaoImpl cartDao=new CartDaoImpl();
       // final boolean b = cartDao.dealCartData(id, num, uid);
        boolean b = false;

        try {
            if ( cartDao.queryCart(id, uid)!= false){
                try {
                    return  cartDao.updateCart(id, num, uid);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }else {
                try {
                    return cartDao.addCart(id, num, uid);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public List<CartUnion> queryCart(String uid) {
        //调用Dao
        CartDaoImpl cartDao=new CartDaoImpl();
        List<CartUnion> list=   cartDao.queryCartList(uid);
        if (list!=null)
            return list;
        return null;
    }

    @Override
    public int  deleteCart(String cid,String uid) {
        //调用Dao
        CartDaoImpl cartDao=new CartDaoImpl();
        return cartDao.deleteCart(cid,uid);

    }

    @Override
    public boolean clearCart(int uid) {
        //调用Dao
        CartDaoImpl cartDao=new CartDaoImpl();
        final boolean clearResult = cartDao.deleteCartByUserId(uid);
        if(clearResult==true)
            return true;
        return false;

    }
}
